package com.epam.tags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.epam.dao.impl.TouristVoucherDAO;
import com.epam.entity.Hotel;
import com.epam.entity.Purchase;
import com.epam.entity.TouristVoucher;
import com.epam.entity.TouristVoucher.Country;
import com.epam.entity.TouristVoucher.Transport;
import com.epam.entity.TouristVoucher.Type;

public class PurchaseTagHandler extends TagSupport {

	private static Logger LOG = Logger.getLogger(PurchaseTagHandler.class);
	private static final long serialVersionUID = 1L;
	private List<Purchase> purchases;

	public int doStartTag() {
		TouristVoucherDAO tvd = new TouristVoucherDAO();
		JspWriter out = pageContext.getOut();

		for (Purchase p: purchases) {
			
			TouristVoucher tv = tvd.findByID(p.getVoucherId());

			Country country = tv.getCountry();
			int duration = tv.getDuration();
			Type type = tv.getType();
			Transport transport = tv.getTransport();
			Hotel hotel = tv.getHotel();
			int cost = tv.getCost();
			String date = p.getDate().toString();

			try {
				out.println("<h1 style='border: 1px black solid; font-size: 15px;'>");
				out.println("Purchase ID: " + p.getPurchaseId() + " |");
				out.println("Country: " + country + " |");
				out.println("Duration: " + duration + " days" + " |");
				out.println("Type: " + type + " |");
				out.println("Transport: " + transport + " |");
				out.println("Hotel: " + hotel.getName() + " |");
				out.println("Cost: " + cost + "$" + " |");
				out.println("Purchase date: " + date + "<p>");
				out.println("</h1>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				LOG.error(e);
			}
		} return SKIP_BODY;
	}

	public List<Purchase> getPurchases() {
		return purchases;
	}

	public void setPurchases(List<Purchase> purchases) {
		this.purchases = purchases;
	}

	
}
